package com.qf.controller;

import com.qf.entity.CarLocation;
import com.qf.entity.CarMessage;
import com.qf.feign.CarLocationFeign;
import com.qf.service.CarMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
public class CarMessageController {
    @Autowired
    private CarMessageService carMessageService;
    @Autowired
    private CarLocationFeign carLocationFeign;

    @RequestMapping("add")
    @ResponseBody
    @CrossOrigin
    public String addCarMessage(CarMessage carMessage){

        ArrayList<CarLocation> carLocations = new ArrayList<>();
        System.out.println(carMessage);

        int i = carMessageService.addCarMessage(carMessage);
        List<CarLocation> list = carLocationFeign.selAll();
        for (CarLocation carLocation : list) {

            if (carLocation.getStatued()==1){
                continue;
            }
            carLocations.add(carLocation);

        }
        if (carLocations.size()!=0){
            Random random = new Random();
            int index = random.nextInt(carLocations.size());
            CarLocation carLocation = carLocations.get(index);
            System.out.println(carLocation);
            if (i>0){
                return "你好已经为你找到一个车位，车的位置在"+carLocation.getLocation();
            }
        }





        return "对不起,当前没有车位提供给你";
    }
    @RequestMapping("sel")
    @ResponseBody
    public List<CarLocation> selAll(){
        return carLocationFeign.selAll();
    }




}
